package com.tepth.maintenancesystem.view;

import android.content.Context;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.Switch;
import android.widget.TextView;

import com.tepth.maintenancesystem.R;
import com.tepth.maintenancesystem.SysApplication;
import com.tepth.maintenancesystem.api.MaintainHttp;
import com.tepth.maintenancesystem.bean.Contents;
import com.tepth.maintenancesystem.bean.MaintainRefer;
import com.tepth.maintenancesystem.untils.ToastUtils;
import com.tepth.maintenancesystem.untils.Untils;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class MaintainWorkDetailsActivity extends AppCompatActivity {

    @BindView(R.id.tv_work_details_title)
    TextView tv_work_details_title;

    @BindView(R.id.tv_work_details_content_name)
    TextView tv_work_details_content_name;

    @BindView(R.id.tv_work_details_content_desc)
    TextView tv_work_details_content_desc;

    @BindView(R.id.tv_work_details_requirement)
    TextView tv_work_details_requirement;

    @BindView(R.id.linear_switch)
    LinearLayout linear_switch;

    @BindView(R.id.switch_work_details)
    Switch switch_work_details;

    @BindView(R.id.linear_edit_number)
    LinearLayout linear_edit_number;

    @BindView(R.id.edit_number_work_details)
    EditText edit_number_work_details;

    @BindView(R.id.tv_number_work_details)
    TextView tv_number_work_details;

    @BindView(R.id.edit_work_details)
    EditText edit_work_details;

    @BindView(R.id.linear_requirement)
    LinearLayout linear_requirement;

    private Contents obj;
    private Context context;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_maintain_work_details);
        SysApplication.getInstance().addActivity(this);
        context = this;
        ButterKnife.bind(this);
        Bundle bundle = getIntent().getBundleExtra("bundle");
        obj = bundle.getParcelable("contents");
        String workName = bundle.getString("workName");
        String vehicleName = bundle.getString("vehicleName");
        tv_work_details_title.setText(vehicleName + " 作业详情");
        tv_work_details_content_name.setText(workName);
        tv_work_details_content_desc.setText(obj.getContentDesc());
        tv_work_details_requirement.setText(obj.getTechnicalRequirement());
        if (obj.getValueTypeCode().equals("select")) {
            linear_switch.setVisibility(View.VISIBLE);
            linear_edit_number.setVisibility(View.GONE);
            List<MaintainRefer> refers = Untils.getMaintianRefer(context);
            for (int i = 0; i < refers.size(); i++) {
                if (refers.get(i).getSelectType().equals(obj.getValueTypeDetail())) {
                    List<MaintainRefer.Selection> selections = refers.get(i).getSelections();
                    for (int j = 0; j < selections.size(); j++) {
                        if (obj.getValue().equals(selections.get(j).getCode())) {
                            switch_work_details.setChecked(selections.get(j).getName().equals("正常"));
                            break;
                        }
                    }
                }
            }
            linear_requirement.setVisibility(View.VISIBLE);
            if (obj.getRemark().equals("null")) {
                edit_work_details.setText("");
            } else {
                edit_work_details.setText(obj.getRemark());
            }
        } else if (obj.getValueTypeCode().equals("numeric")) {
            linear_switch.setVisibility(View.GONE);
            linear_edit_number.setVisibility(View.VISIBLE);
            if (obj.getMaxValue().equals("") && obj.getMinValue().equals("")) {
                tv_number_work_details.setText("数值");
            } else if (obj.getMaxValue().equals("")) {
                tv_number_work_details.setText("数值（最小值: " + obj.getMinValue() + "）");
            } else if (obj.getMinValue().equals("")) {
                tv_number_work_details.setText("数值（最大值: " + obj.getMaxValue() + "）");
            } else {
                tv_number_work_details.setText("数值（" + obj.getMinValue() + " - " + obj.getMaxValue() + "）");
            }
            edit_number_work_details.setText(obj.getValue());
            if (obj.getRemark().equals("null")) {
                edit_work_details.setText("");
            } else {
                edit_work_details.setText(obj.getRemark());
            }
            linear_requirement.setVisibility(View.VISIBLE);
        } else {
            linear_switch.setVisibility(View.GONE);
            linear_edit_number.setVisibility(View.GONE);
            findViewById(R.id.view_type_visible).setVisibility(View.GONE);
            if (obj.getValue().equals("null")) {
                edit_work_details.setText("");
            } else {
                edit_work_details.setText(obj.getValue());
            }
            linear_requirement.setVisibility(View.GONE);
        }
    }

    @OnClick({R.id.linear_work_details, R.id.rl_work_details_back})
    public void onclick(View view) {
        switch (view.getId()) {
            case R.id.linear_work_details:
                String value = "";
                if (obj.getValueTypeCode().equals("select")) {
                    List<MaintainRefer> refers = Untils.getMaintianRefer(context);
                    for (int i = 0; i < refers.size(); i++) {
                        if (refers.get(i).getSelectType().equals(obj.getValueTypeDetail())) {
                            List<MaintainRefer.Selection> selections = refers.get(i).getSelections();
                            for (int j = 0; j < selections.size(); j++) {
                                if (switch_work_details.isChecked()) {
                                    if (selections.get(j).getName().equals("正常")) {
                                        value = selections.get(j).getCode();
                                        break;
                                    }
                                } else {
                                    if (selections.get(j).getName().equals("异常")) {
                                        value = selections.get(j).getCode();
                                        break;
                                    }
                                }
                            }
                        }
                    }
                    obj.setValue(value);
                    obj.setRemark(edit_work_details.getText().toString());
                } else if (obj.getValueTypeCode().equals("numeric")) {
                    obj.setRemark(edit_work_details.getText().toString());
                    if (edit_number_work_details.getText().toString().equals("")) {
                        obj.setValue(obj.getValueDefault());
                    } else {
                        obj.setValue(edit_number_work_details.getText().toString());
                    }
                } else {
                    obj.setValue(edit_work_details.getText().toString());
                }
                Intent intent = new Intent();
                Bundle bundle = new Bundle();
                bundle.putParcelable("MaintainObj", obj);
                intent.putExtra("maintainBundle", bundle);
                setResult(MaintainHttp.RESULT_REFERSHOBJECT, intent);
                this.finish();
                break;
            case R.id.rl_work_details_back:
                this.finish();
                break;
        }
    }
}
